package hengtiansoft.com.myapplication;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;


/**
 * Created by lu on 16/1/25.
 */
public class UrlView extends Activity {
    ImageView myView;
    Bitmap bitmap;
    Boolean isBack;
    WindowManager myWindow;
    Button view;

    Handler handler = new Handler() {
        public void handleMessage(Message mag) {
            if (mag.what == 0) {
                myView.setImageBitmap(bitmap);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.urlview);
        initView();
    }

    private void initView() {
//        myWindow = (WindowManager) getApplicationContext().getSystemService(
//                Context.WINDOW_SERVICE);
//        view = new Button(this);
//        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
//        params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
//        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
//        params.gravity = Gravity.CENTER;
//        myWindow.addView(view, params);
        WindowUtil.showPopupWindow(UrlView.this);
        myView = (ImageView) findViewById(R.id.urview);
        new Thread() {
            @Override
            public void run() {
                try {
                    Log.d("lu", "" + System.currentTimeMillis());
                    URL url = new URL("http://pic1a.nipic.com/2008-09-19/200891903253318_2.jpg");
                    Log.d("lu", "" + System.currentTimeMillis());
                    InputStream is = url.openStream();
                    Log.d("lu", "" + System.currentTimeMillis());
                    bitmap = BitmapFactory.decodeStream(is);
                    Log.d("lu4", "" + System.currentTimeMillis());
                    handler.sendEmptyMessage(0);
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                super.run();
            }
        }.start();
        myView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                WindowUtil.showPopupWindow(UrlView.this);
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        myWindow.removeView(view);
        if (isBack) System.exit(0);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            isBack = true;
        }
        return super.onKeyDown(keyCode, event);

    }
}
